package com.example.coffeeseller_2.widget;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.Button;
import android.widget.LinearLayout;

import com.example.coffeeseller_2.R;

/**
 * Created by liyongapple on 2018/10/11.
 */

public class KeyBoardView extends LinearLayout {
    private static final String [] KEY_LIST= {"1","2","3","4","5","6","7","8","9","0",
                                            "Q","W","E","R","T","Y","U","I","O","P",
                                            "A","S","D","F","G","H","J","K","L",
                                            "Z","X","C","V","B","N","M","删除"};

    private int keyMargin = 16;//按键左间距 单位dp
    private int rowMargin = 22;//行顶部间距
    private int keyClolrNormal = Color.parseColor("#CF9E7D");
    private int keyClolrPress = Color.parseColor("#FFFFFF");
    private int keyTextSize = 28;
    private int keyBtnBackgroundResource =  R.drawable.selecter_keyview_btn;

    private KeyEventListener keyEventListener;

    public KeyBoardView(Context context) {
        this(context,null);
    }

    public KeyBoardView(Context context, AttributeSet attrs) {
        this(context,attrs,0);
    }

    public KeyBoardView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setOrientation(VERTICAL);
        setGravity(Gravity.CENTER);
        initView();
    }

    private void initView() {

        addView(addRow(0,10));
        addView(addRow(10,10));
        addView(addRow(20,9));
        addView(addRow(29,KEY_LIST.length-29));

        getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                getViewTreeObserver().removeGlobalOnLayoutListener(this);
                LinearLayout row1= (LinearLayout) getChildAt(0);
                int width = row1.getChildAt(0).getWidth();
                int height = (getHeight()-getPaddingTop()-getPaddingBottom()-3*dp2px(rowMargin))/4;
                for(int i=0;i<4;i++){
                    LinearLayout row= (LinearLayout) getChildAt(i);
                    if(i == 2 || i == 3){
                        row.setPadding(width/2,0,width/2,0);
                    }
                    for(int j=0,k=row.getChildCount();j<k;j++){
                        Button keyBtn = (Button) row.getChildAt(j);
                        LayoutParams layoutParams = (LayoutParams) keyBtn.getLayoutParams();
                        layoutParams.height = height;
                        if(i == 3 && j == k-1){
                            layoutParams.weight = (float) (2+(dp2px(keyMargin)/(width*1.0))/8*7);
                        }
                        keyBtn.requestLayout();
                    }
                }
            }
        });
    }
    private LinearLayout addRow(int position,int count){
        LinearLayout linearLayout = new LinearLayout(getContext());
        linearLayout.setOrientation(HORIZONTAL);
        linearLayout.setGravity(Gravity.CENTER);
        if(position != 0) {
            LayoutParams p = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
            p.setMargins(0, dp2px(rowMargin), 0, 0);
            linearLayout.setLayoutParams(p);
        }
        for(int i=0;i<count;i++) {
            Button keyView = new Button(getContext());
            keyView.setPadding(0,0,0,0);
            keyView.setBackgroundResource(keyBtnBackgroundResource);
            keyView.setText(KEY_LIST[position+i]);
            keyView.setTextColor(keyClolrNormal);
            keyView.setTextSize(keyTextSize);
            LayoutParams param = new LayoutParams(0, LayoutParams.MATCH_PARENT, 1.0f);
            if(i==0){
                param.setMargins(0, 0, 0, 0);
            }else{
                param.setMargins(dp2px(keyMargin), 0, 0, 0);
            }
            keyView.setLayoutParams(param);
            setClick(keyView);
            linearLayout.addView(keyView);
        }
        return linearLayout;
    }

    private void setClick(Button keyView) {

        keyView.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                Button keyView = (Button) view;
                switch (motionEvent.getAction()){
                    case MotionEvent.ACTION_DOWN:
                        keyView.setTextColor(keyClolrPress);
                        break;
                    case MotionEvent.ACTION_UP:
                        keyView.setTextColor(keyClolrNormal);
                        if(keyEventListener == null){
                            return false;
                        }
                        String keyValue = keyView.getText().toString();
                        if(keyValue.equals("删除")){
                            keyEventListener.delete();
                        }else{
                            keyEventListener.onKey(keyValue);
                        }
                        break;
                }
                return false;
            }
        });

    }

    private int dp2px(float dipValue) {
        final float scale = getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }
    public int px2dp(float pxValue) {
        final float scale = getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }
    public void setOnKeyEventListener(KeyEventListener listener){
        keyEventListener = listener;
    }
    public interface KeyEventListener{
        void onKey(String key);
        void delete();
    }

    public KeyBoardView setKeyMargin(int keyMargin) {
        this.keyMargin = keyMargin;
        return this;
    }

    public KeyBoardView setRowMargin(int rowMargin) {
        this.rowMargin = rowMargin;
        return this;
    }

    public KeyBoardView setKeyClolrNormal(int keyClolrNormal) {
        this.keyClolrNormal = keyClolrNormal;
        return this;
    }

    public KeyBoardView setKeyClolrPress(int keyClolrPress) {
        this.keyClolrPress = keyClolrPress;
        return this;
    }

    public KeyBoardView setKeyTextSize(int keyTextSize) {
        this.keyTextSize = keyTextSize;
        return this;
    }

    public KeyBoardView setKeyBtnBackgroundResource(int keyBtnBackgroundResource) {
        this.keyBtnBackgroundResource = keyBtnBackgroundResource;
        return this;
    }
    public void build(){
        removeAllViews();
        initView();
    }

}
